package com.tl.khadmin.service.impl;
 
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Service; 

import com.tl.khadmin.bean.ProductBrand;
import com.tl.khadmin.bean.Staff;
import com.tl.khadmin.bean.Supplier;
import com.tl.khadmin.mapper.ProductBrandMapper; 
import com.tl.khadmin.page.Page;
import com.tl.khadmin.service.ProductBrandIService;
import com.tl.khadmin.util.CommonUtil;
import com.tl.khadmin.util.Constant; 

@Service
public class ProductBrandServiceImpl implements ProductBrandIService{
	
	@Resource
	private ProductBrandMapper productBrandMapper;
	
	public List<ProductBrand> listProductBrand(Map<String, Object> param){
		return productBrandMapper.listProductBrand(param); 
	}
	
	@Override
	public String updateProductBrand(ProductBrand productBrand) {
		Integer id = productBrandMapper.queryIdByBrandNameAndId(productBrand.getId(), productBrand.getBrandName());
		if (id != null) {
			return CommonUtil.returnMapResultToJson("fail","品牌名称已经存在");
		}  
		int result = productBrandMapper.updateByPrimaryKeySelective(productBrand);
		return CommonUtil.returnMapResultByRows(result, "修改成功", "修改失败，请重试");
	}
	
	@Override
	public Page<ProductBrand> showProductBrandByPage(Integer pageNum, Integer pageSize, Map<String, Object> params) {
		Page<ProductBrand> page=new Page<ProductBrand>(pageNum, pageSize);
        params.put("page", page);   
        List<ProductBrand> list= productBrandMapper.showProductBrandByPage(params);  
		page.setResult(list);
		return page;
	}

	@Override
	public String saveProductBrand(HttpSession session, ProductBrand productBrand) {
		Integer id = productBrandMapper.findIdByBrandName(productBrand.getBrandName());
		if (id != null) {
			return CommonUtil.returnMapResultToJson("fail","品牌名称已经存在");
		} 
		Staff staff = (Staff) session.getAttribute(Constant.USER);
		Supplier supplier = (Supplier) session.getAttribute(Constant.USER_SUPPLIER); 
		productBrand.setCreateId(staff.getId()); 
		productBrand.setCreateTime(new Date()); 
		int result = productBrandMapper.insertSelective(productBrand);
		return CommonUtil.returnMapResultByRows(result, "添加成功", "添加失败，请重试");
	}

//
//
//	@Override
//	public Page<ShopProduct> showProductByPage(Integer pageNum, Integer pageSize, Map<String, Object> params) {
//		
//		Page<ShopProduct> page=new Page<ShopProduct>(pageNum, pageSize);
//        params.put("page", page);   
//        List<ShopProduct> list= shopProductMapper.showProductByPage(params);  
//		page.setResult(list);
//		return page;
//	}
//
//	@Override
//	public int deleteProductById(HttpSession session,Integer id) {
//		List<ShopProductImg> list = shopProductImgMapper.selectByproductId(id);
//		Integer ret1 = shopProductImgMapper.selectCountByproductId(id);
//		Integer ret2 = shopProductImgMapper.deleteImgByProductId(id);
//		if(ret1 == ret2){
//			Integer ret3 = shopProductMapper.deleteByPrimaryKey(id);
//			if(1 == ret3){
//				String path = session.getServletContext().getRealPath("/");
//				for(int i=0;i<list.size();i++){
//					String root = path + list.get(i).getImgpath();
//					File  file = new File(root);  
//			        // 路径为文件且不为空则进行删除  
//			        if (file.isFile() && file.exists()) {  
//			            file.delete();  
//			        }
//				}
//				return 1;
//			}
//			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//事物回滚
//		}
//		return 0;
//	}
//
	@Override
	public String deleteByIds(String ids) {
		String[] idsArr = ids.substring(0,ids.length()-1).split(",");
		try{
			int result = productBrandMapper.deleteByIds(idsArr);
		}catch(Exception ex) {
			return CommonUtil.returnMapResultByRows(0, "删除成功","删除失败，请重试");
		} 
		return CommonUtil.returnMapResultByRows(1, "删除成功","删除失败，请重试");
	}

	@Override
	public Map<String, Object> queryProductBrandById(Integer id) {
		Map<String, Object> result = new HashMap<>();
		ProductBrand productBrand = productBrandMapper.selectByPrimaryKey(id); 
		result.put("productBrand", productBrand); 
		return result;
	}

//	@Override
//	public Map<String, Object> queryProductById(Integer id) {
//		
//		Map<String, Object> result = new HashMap<>();
//		ShopProduct shopProduct = shopProductMapper.selectByPrimaryKey(id);
//		List<ShopProductImg> shopProductImgs = shopProductImgMapper.selectByproductId(id);
//		result.put("product", shopProduct);
//		result.put("imglist", shopProductImgs);
//		return result;
//	}
//
//	@Override
//	public Map<String, Object> updateProduct(HttpSession session,ShopProduct shopProduct,String productImgIds) {
//		
//		Integer result = shopProductMapper.updateByPrimaryKeySelective(shopProduct);
//		Map<String, Object> map = new HashMap<String, Object>();
//		if(1 != result) {
//			map.put("result", "fail");
//			return map;
//		}
//		if(productImgIds != null && productImgIds.length() > 0){
//			String[] ids = productImgIds.substring(0,productImgIds.length()-1).split(",");
//			List<ShopProductImg> list = shopProductImgMapper.selectByIds(ids);
//			int ret = shopProductImgMapper.deleteImgByIds(ids);
//			if(ids.length != ret){
//				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//事物回滚
//				map.put("result", "fail");
//				return map;
//			}
//			String path = session.getServletContext().getRealPath("/");
//			for(int i=0;i<list.size();i++){
//				String root = path + list.get(i).getImgpath();
//				File  file = new File(root);  
//		        // 路径为文件且不为空则进行删除  
//		        if (file.isFile() && file.exists()) {  
//		            file.delete();  
//		        }
//			}
//		}
//		map.put("result", "success");
//		return map;
//	}
//
//	@Override
//	public Integer deleteProductImgByProductId(Integer productid) {
//		
//		return shopProductImgMapper.deleteImgByProductId(productid);
//	}
//
//	@Override
//	public int queryCountByProductId(Integer productid) {
//
//		return shopProductImgMapper.selectCountByproductId(productid);
//	}
//
//	@Override
//	public int updateState(Integer id, Integer state) {
//		ShopProduct shopProduct = new ShopProduct();
//		shopProduct.setId(id);
//		shopProduct.setState(state);
//		return shopProductMapper.updateByPrimaryKeySelective(shopProduct);
//	}
//
//	@Override
//	public int updateProductImg(HttpSession session,ShopProductImg productImg) {
//		Map<String,Object> map = new HashMap<>();
//		map.put("productid", productImg.getProductid());
//		map.put("type", productImg.getType());
//		ShopProductImg shopProductImg = shopProductImgMapper.selectByKey(map);
//		String path = session.getServletContext().getRealPath("/");
//		path = path + shopProductImg.getImgpath();
//		File  file = new File(path);  
//        // 路径为文件且不为空则进行删除  
//        if (file.isFile() && file.exists()) {  
//            file.delete();  
//        }
//        ShopProduct newProduct = new ShopProduct();
//		newProduct.setId(productImg.getProductid());
//		newProduct.setImgurl(productImg.getImgpath());
//		Integer result = shopProductMapper.updateByPrimaryKeySelective(newProduct);
//		if(result!=1){
//			return result;
//		}
//		return shopProductImgMapper.updateImgByProductId(productImg);
//	}

}
